<?php
	function insertImage ( $p_album_id , $p_image_name , $p_image_location, $p_account_id ){
		$sql = 'INSERT INTO TB_IMAGE (ALBUM_ID, IMAGE_NAME, IMAGE_LOCATION, ACCOUNT_ID) VALUES
				("'.$p_album_id.'"
				, "'.mysql_real_escape_string($p_image_name).'"
				, "'.$p_image_location.'"
				, "'.$p_account_id.'"
				)';
		if ( mysql_query($sql) ){
			return mysql_insert_id();
		}
		return false;
	}
	function getAllImageBasedOnAlbum ($p_album_id){
		$sql = 'SELECT IMAGE_ID, IMAGE_NAME , IMAGE_LOCATION , ACCOUNT_AVATAR
				FROM TB_IMAGE
				WHERE ALBUM_ID = "'.$p_album_id.'"';
		$rs = mysql_query($sql) or die( mysql_error() );
		return $rs;
	}

	function generateAlbumSlider ($p_album_id){
		$rs = getAllImageBasedOnAlbum ($p_album_id);
		$v_dsp = '';
		$i = 0;
		while ( $arr = mysql_fetch_assoc($rs) ){
			$i ++;
			$v_image_id = $arr['IMAGE_ID'];
			$v_image_name = $arr['IMAGE_NAME'];
			$v_image_path = 'lib/upload/'.$arr['IMAGE_LOCATION'];
			$v_image_thum_path = 'lib/upload/thum/'.$arr['IMAGE_LOCATION'];
			$v_dsp .= dspImagesChangeSize( $v_image_thum_path,' href="'.URL.$v_image_path.'" rel="alb'.$p_album_id.'" title="'.$v_image_name.'" alt="'.$v_image_name.'" id= "img_'.$v_image_id.'" class="image_slider"',3 );
			
		}
		return $v_dsp;
	}

	function getCoupleImage ($account_id){
		$sql = " SELECT IMAGE_LOCATION
				FROM TB_IMAGE
				WHERE ACCOUNT_ID = '".$account_id."' AND ACCOUNT_AVATAR = 1 " ;
		$rs = mysql_query($sql) or die (mysql_error());
		if ($arr = mysql_fetch_assoc($rs))
			return $arr['IMAGE_LOCATION'];
	}

	function deleteImage($p_image_id){
		$sql = ' SELECT IMAGE_LOCATION
				FROM TB_IMAGE
				WHERE IMAGE_ID = '.$p_image_id ;
		$rs = mysql_query($sql) or die (mysql_error());
		if ( $arr = mysql_fetch_assoc($rs) ){
			$img_path = 'lib/upload/'.$arr['IMAGE_LOCATION'];
			$img_thum_path = 'lib/upload/thum/'.$arr['IMAGE_LOCATION'];
			
			unlink($img_path);
			unlink($img_thum_path);
			mysql_query(" DELETE FROM TB_IMAGE WHERE IMAGE_ID =  '".$p_image_id."'	 ")or die(mysql_error());
			return true;
			
		}
		return false;
	}
	function dspImagesChangeSize($p_image_link, $extra='', $p_type=1) {
	switch ($p_type) {
		case '1';
			$max_width = 0;
			$max_height = 150;
			break;
		case '2';
			$max_width = 550;
			$max_height = 400;
			break;
		case '3';
			$max_width = 125;
			$max_height = 125;
			break;
		case "4";
			$max_width = 200;
			$max_height = 200;
			break;
		case "5";
			$max_width = 0;
			$max_height = 140;
			break;
		case '6';
			$max_width = 100;
			$max_height = 0;
			break;
	}

	if (file_exists($p_image_link)) {
		list($width, $height) = getimagesize($p_image_link);
		$ratioh = $max_height / $height;
		$ratiow = $max_width / $width;
		
		if ($ratioh == 0 && $ratiow!=0 )
			$ratio = $ratiow;
		elseif ($ratiow == 0 && $ratioh!=0 )
			$ratio = $ratioh;
		else
			$ratio = min($ratioh, $ratiow);
		
		// New dimensions 
		$width = intval($ratio * $width);
		$height = intval($ratio * $height);
		return '<img '.$extra.' height="' . $height . '" width="' . $width . '" src="' . URL . $p_image_link . '"/>';
	}
	return '';
}